package com.suitsoft.ut.web.middle.dabian;


import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.google.common.collect.Lists;
import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.account.AccountManager;
import com.suitsoft.ut.service.commons.SpotCheckManager;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 抽查
 *@author Suineg
 */
@SuppressWarnings("serial")
@Results({@Result(name = CrudActionSupport.RELOAD,location="spot-check",type="redirect")})
public class SpotCheckAction extends InitListActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** 随机抽查论文 服务层对象 */
	private SpotCheckManager checkManager;
	/** 流程管理  服务层对象 */
	private ThesisFileManager fileManager;
	
	private List<Thesis> listAll = Lists.newArrayList();
	/** 人数 */
	private Integer cnt;
	/** 百分比 */
	private float percent;
	/** 已选ID */
	private List<Long> checkedId;
	/***  */
	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	
	@Override
	public String execute() throws Exception {
		Teacher teacher = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(teacher == null){
			addActionMessage("请使用教师账号");
			return SUCCESS;
		}
		intiFlagStatus();
		Struts2Utils.getRequest().setAttribute("size", listAll.size());
//		List<Thesis> list = selectManager.findSelcted();
		List<Thesis> list = selectManager.findSelectedForMiddle();
		listAll = null;
		Struts2Utils.getRequest().setAttribute("lisfdas", list);
		return SUCCESS;
	}
	
	/**
	 * 获取到中期环节的所有论文
	 */
	private void intiFlagStatus(){
		List<Long> falsgss = Lists.newArrayList();
		falsgss.add(Constants.THESIS_FLAG_KAITI);
		falsgss.add(Constants.THESIS_FLAG_MIDLLE);
		
		List<Long> stautss = Lists.newArrayList();
		stautss.add(Constants.THSIS_STAUTS_KAITI_BASE_AUDIT_REPORT);
		stautss.add(Constants.THSIS_STAUTS_MIDLLE_WEEK_REPORT);
		stautss.add(Constants.THSIS_STAUTS_MIDLLE_READ_WEEK_REPORT);
		listAll = selectManager.findExaminationBy(null, null, null, falsgss, stautss, null, null);
	}
	/**
	 * 获取随机论文
	 * @return
	 */
	public String spotCheck(){
		if(cnt == null && percent == 0.0){
			addActionMessage("至少选一个");
			return SUCCESS;
		}
		intiFlagStatus();
		List<Thesis> list = selectManager.findSelectedForMiddleByUser(UserHolder.getLoginUser().getCollegeCode());
		listAll = checkManager.random(listAll, cnt, percent);
		return SUCCESS;
	}
	/**
	 * 抽查答辩学生
	 * @return
	 */
	@LogAnnotation(log = "抽查答辩学生 - 确定抽查答辩学生名单")
	public String confirmStudent(){
		if(checkedId == null || checkedId.size() == 0){
			addActionMessage("请至少选择一个随机选出来的学生");
			return SUCCESS;
		}
		try {
			//把被抽中的学生设置成  已被选 状态
			List<Thesis> list = thesisManager.get(checkedId);
			ThesisFile file = null;
			for(Thesis t : list){
				file = t.getThesisFile();
				file.setIsSelect(1L);
				file.setSelectedDate(new Date());
				file.setSelectedName(teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId()).getTeacherName());
				fileManager.save(file);
			}
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
		}
		return SUCCESS;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public List<Thesis> getListAll() {
		return listAll;
	}
	public void setListAll(List<Thesis> listAll) {
		this.listAll = listAll;
	}
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	public Integer getCnt() {
		return cnt;
	}
	public void setCnt(Integer cnt) {
		this.cnt = cnt;
	}
	public float getPercent() {
		return percent;
	}
	public void setPercent(float percent) {
		this.percent = percent;
	}
	@Autowired
	public void setCheckManager(SpotCheckManager checkManager) {
		this.checkManager = checkManager;
	}

	public List<Long> getCheckedId() {
		return checkedId;
	}

	public void setCheckedId(List<Long> checkedId) {
		this.checkedId = checkedId;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}

	public Page<Thesis> getPage() {
		return page;
	}

	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
}

